import {esri} from "../api/esriAPI";

export const mapLinkage = {};
var saveInfos = {};
//绑定
mapLinkage.linkage = function (key, arcgisTools = []) {
  if (saveInfos[key]) {
    console.error("该key已经存在，请重新命名");
    return;
  }
  let events = [];
  let index = 0;
  let len = arcgisTools.length;

  function updateMap(a, v, c, evt) {
    index = ++index % len;
    if (index) {
      return;
    }
    console.log("xxx",index,len, evt)
    let curId = evt.container.id;//例如:test_2D
    for (let arcgisTool of arcgisTools) {
      let view = arcgisTool.manageView.getView();
      let id = view.container.id;
      if (curId != id) {
        // view.viewpoint = el.viewpoint;
        //view.extent = evt.extent;
        view.goTo(evt.extent, {
          // speedFactor: 0.1,
          // duration:200,
          // easing: "out-quint"
        });
      }
    }
  }

  for (var arcgisTool of arcgisTools) {
    let mainView = arcgisTool.manageView.getView();
    // 主视图静止时更新其他地图
    let event = esri.watchUtils.when(mainView, "stationary", updateMap.bind(this));
    events.push(event)
  }
  saveInfos[key] = {events};

}
//解绑所有
mapLinkage.unLinkageAll = function () {
  for (let key in saveInfos) {
    this.unLinkage(key);
  }
  saveInfos = {};
}
//解绑
mapLinkage.unLinkage = function (key) {
  var {events} = saveInfos[key];
  for (let index in events) {
    events[index].remove();
  }
  saveInfos[key] = null;
}
